<?php
/********************
 *@param function:基本設置 model
 *@param datetime 2011-6-7
 *@param author solgh
 ********************/
class Operator_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_operator';

  function __construct(){
    parent::__construct();
    log_message('debug','operator_mdl Model class Initialized');
  }

  /****
   *查詢所operator,加分页
   *$data :查询参数.
   */
  public function get_operator($data=array()){
    extract($data);
    isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('add_time DESC');
    isset($operator) && !empty($operator)?$this->db->where('operator',$operator):'';
    isset($title) && !empty($title)?$this->db->where('title',$title):'';
    isset($action) && !empty($action)?$this->db->where('action',$action):'';
    isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
    return $this->db->get(self::TAB_NAME);
    
  }
  /****
   * 統計所有的operator
   */
  public function totle_operator($data=array()){
    if(!empty($data)){
      extract($data);
      isset($operator) && !empty($operator)?$this->db->where('operator',$operator):'';
      isset($title) && !empty($title)?$this->db->where('title',$title):'';
      isset($action) && !empty($action)?$this->db->where('action',$action):'';
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /*****
   * 根據ID來刪除
   */
  public function delete_operator($operator_id=''){
    if(!empty($operator_id)){
      $this->db->where("id",$operator_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 修改一個operator
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_operator($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 operator
   */
  public function add_operator($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 清空 operator
   */
  public function empty_table(){
    $this->db->empty_table(self::TAB_NAME);
    return ($this->db->affected_rows()>0)?true:false;
  }
  
  /*****
   * 根據設置天數來刪除
   */
  public function delete_logs($expired=''){
    if(!empty($expired)){
      $this->db->where("add_time<",$expired,FALSE);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
}
